<?
    class OptionPanel extends HTMLControl {
        private $_validchars = "abcdefghijklmnopqrstuvwxyz0123456789";
        private $_table;
        private $_disabled = false;
        private $_comments = '';
        private $_childnames = array();
        
        function __construct($label,$labelischeckable = false) {
            parent::__construct("fieldset");
            
            $this->SetStyle("margin-bottom","15px");
            $this->attributes->name = SettingManager::EncodeSettingName($label);
            $legend = new HTMLControl("legend");
            
            if ($labelischeckable) {
                $labelcontrol = new HTMLControl("label",$label);
                $checkbox = new HTMLControl("input");
                $checkbox->attributes->type = "checkbox";
                $checkbox->attributes->value = "1";
                $checkbox->attributes->name = SettingManager::EncodeSettingName($label) . "_enabled";
                
                if (SettingManager::GetSetting($checkbox->attributes->name)) $checkbox->attributes->checked = "checked";
                else {
                    $this->_disabled = true;
                }
                
                $checkbox->attributes->onClick = "toggleEnabled(\"" . $this->attributes->name . "\")"; 
                $labelcontrol->AddChild($checkbox);
                
                $legend->AddChild($labelcontrol);
            }
            else 
                $legend->AddChild(new HTMLControl("span",$label));
            
            $this->AddChild($legend);
            $this->_table = new HTMLControl("table");
            $this->_table->attributes->width = "100%";
        }
        
        function AddComment($text) {
            $this->_comments .= $text . "<br>";
        }
        
        function AddOption($label,FormElement $control) {
            // for some other coo stuff
            // setting the ID allows us to enable and disable entire option panels
            // instead of just turning the background of the panel gray
            
            if ($control->attributes->name != $control->attributes->id) {
                $control->attributes->id = $control->attributes->name;
            }
        
            $holder = new HTMLControl("tr");
            
            $labelcell = new HTMLControl("td",null,array("class","label"),array("border-bottom"=>"solid 1px gray"));
            $labelcell->SetStyle("text-align","right");
            $title = new HTMLControl("div",$label,null,array("text-align"=>"right"));
            $value = new HTMLControl("span",$control->attributes->name,array("class" => "sublabel"));
            
            $labelcell->AddChild($title);
            if (SettingManager::GetSetting("show_varnames")) $labelcell->AddChild($value);
            $labelcell->styles->width = "25%";
            $labelcell->SetAttribute("class","label");
            $holder->AddChild($labelcell);
            
            $controlcell = new HTMLControl("td");
            $controlcell->AddChild($control);
            $this->_childnames[] = $control->attributes->name;
            if ($this->_disabled)
                $control->attributes->disabled = "disabled";
            $holder->AddChild($controlcell);
            
            $this->_table->AddChild($holder);
        }
        
        function ChildOptions() {
            return $this->_childnames;
        }
        
        function Render($return = false) {
            if ($this->_comments) {
                $comments = new HTMLControl('span',$this->_comments);
                $comments->SetStyle("font-size","8pt");
                $this->AddChild($comments);
                $this->AddChild(new HTMLControl("hr"));
            }
            $this->AddChild($this->_table);
            $this->attributes->id = $this->attributes->name;
            if ($this->_disabled) $this->attributes->disabled = "disabled";
            
            return parent::Render($return);
        }
    }
?>